import { computed, ref } from "vue";
import { defineStore } from "pinia";


//对象类型数组
const objectArr: object[] = [];

export interface dateInfoInterface {
  id: string,
  name: string,
  usedQuick: boolean,// 快捷输入
  solarDate?: string,//输入日期，阳历日期
  lunarDate?: string,//农历日期

  isLunar: boolean,//是否是农历日期
  isTop: boolean,//顶部展示
  // repeat:string,//定期提醒暂时不做
}


export const useCommonStore = defineStore("commonStore", () => {

    //state
    const theme = ref("light");
    const title = ref({
      name: "Days Matter",
      hasBack: true
    });


    const userData = ref({
      username: "",
      phone: ""
    });


    const dateInfoList = ref(objectArr);


    //getter
    const isLightTheme = computed(() => theme.value === "light");


    //action
    function setTheme(themeParam: string) {
      theme.value = themeParam || theme.value;

      document.documentElement.setAttribute("theme", theme.value);
    }

    /**
     * 设置日期
     * @param dateInfo
     */
    function setDateInfo(dateInfo: dateInfoInterface) {

      dateInfo.id = getId();

      if (dateInfo.isTop) {
        dateInfoList.value.unshift(dateInfo);
      } else {
        dateInfoList.value.push(dateInfo);
      }

    }

    function deleteDate(index: number) {
      dateInfoList.value.splice(index, 1);
    }

    function getId() {
      return new Date().getTime() + (100000 * Math.random()).toFixed(0);
    }

    return { userData, dateInfoList, theme, isLightTheme, setDateInfo, setTheme, deleteDate, title };
  },
  {
    persist: {
      enabled: true,
      strategies: [{
        key: "commonStore",
        storage: sessionStorage
      }]
    }
  }
);




